package dao;

import domain.Employee;
import utils.DateAndString;
import utils.Utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @autho 3505
 * @date 2018/6/3
 */
public class EmployeeDatabaseDao implements BasicDao{

    @Override
     public int add(Employee employee)   {
        Connection connection=Utils.getConnection();
        String str="insert into employee value(?,?,?,?,?,?,?)";
        PreparedStatement preparedStatement= null;
        try {
            preparedStatement = connection.prepareStatement(str);
            preparedStatement.setInt(1,employee.getEmployeeID());
            preparedStatement.setString(2,employee.getEmployeeName());
            preparedStatement.setString(3,employee.getSex());
            DateAndString d=new DateAndString();
            preparedStatement.setDate(4,new java.sql.Date(employee.getDate().getTime()));
            preparedStatement.setDate(5, new java.sql.Date(employee.getHireDate().getTime()));
            preparedStatement.setDouble(6,Double.valueOf(employee.getSalary()) );
            preparedStatement.setInt(7,employee.getDepartmentID());
            return preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            Utils.close(null,preparedStatement,connection);
        }
        return 0;

    }
    @Override
     public int modify(Employee employee){
        Connection connection=Utils.getConnection();
        String str="UPDATE employee SET salary=? WHERE employeename=?";
        PreparedStatement preparedStatement= null;
        try {
            preparedStatement = connection.prepareStatement(str);
            preparedStatement.setFloat(1,employee.getSalary());
            preparedStatement.setString(2,employee.getEmployeeName());

            return preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            Utils.close(null,preparedStatement,connection);
        }
        return 0;

    }
    @Override
     public int delete(String name){
        Connection connection=Utils.getConnection();
        String str="DELETE FROM employee WHERE employeename=?";
        PreparedStatement preparedStatement= null;
        try {
            preparedStatement = connection.prepareStatement(str);
            preparedStatement.setString(1,name);
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Utils.close(null,preparedStatement,connection);
        }
        return 0;

    }
    @Override
     public Employee searchOne(String name){
        Connection connection=Utils.getConnection();
        String str="SELECT * FROM employee WHERE employeename=?" ;
        PreparedStatement preparedStatement= null;
        ResultSet resultSet=null;
        Employee employee=new Employee();
        try {
            preparedStatement = connection.prepareStatement(str);
            preparedStatement.setString(1,name);
            resultSet=preparedStatement.executeQuery();
            while (resultSet.next()){
                employee=new Employee(resultSet.getInt(1),resultSet.getString(2),resultSet.getString(3),
                        resultSet.getDate(4),resultSet.getDate(5),resultSet.getFloat(6),resultSet.getInt(7));

            }

            return employee;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Utils.close(resultSet,preparedStatement,connection);
        }
        return null;

    }
    @Override
     public List searchAll(){
        Connection connection=Utils.getConnection();
        String str="SELECT * FROM employee " ;
        PreparedStatement preparedStatement= null;
        ResultSet resultSet=null;
        Employee employee=new Employee();
        List list3=new ArrayList();
        try {
            preparedStatement = connection.prepareStatement(str);
            resultSet=preparedStatement.executeQuery();
            while (resultSet.next()){
                employee=new Employee(resultSet.getInt(1),resultSet.getString(2),resultSet.getString(3),
                        resultSet.getDate(4),resultSet.getDate(5),resultSet.getFloat(6),resultSet.getInt(7));
                System.out.println(employee);
                list3.add(employee);
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Utils.close(resultSet,preparedStatement,connection);
        }
        return list3;

    }
}
